{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [],
   "source": [
    "#load \"..\\Extension\\LoadBaseTool.csx\"\n",
    "using System.IO;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Path\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "路径信息：C:\\Sicont\\Test\\1.txt\n",
      "目录信息：C:\\Sicont\\Test\n",
      "目录信息：C:\\Sicont\n",
      "文件名信息：1.txt\n",
      "文件名信息：1\n",
      "扩展名信息：.txt\n",
      "绝对路径：c:\\Users\\zeros\\Documents\\【项目文件夹】\\【常用】草稿文档\\CSharp\\02_CSharp基础\\1.txt\n",
      "临时路径：C:\\Users\\zeros\\AppData\\Local\\Temp\\tmphw2ajm.tmp\n",
      "临时文件：tlg4m2oz.0vo\n",
      "临时文件夹：C:\\Users\\zeros\\AppData\\Local\\Temp\\\n",
      "HasExtension1：True\n",
      "HasExtension2：False\n",
      "HasExtension3：True\n"
     ]
    }
   ],
   "source": [
    "// 路径组合\n",
    "string _path = Path.Combine(@\"C:\\Sicont\", \"Test\", \"1.txt\");\n",
    "Console.WriteLine($\"路径信息：{_path}\");\n",
    "\n",
    "// 获取目录信息\n",
    "string _directory_name = Path.GetDirectoryName(_path);\n",
    "Console.WriteLine($\"目录信息：{_directory_name}\");\n",
    "Console.WriteLine($\"目录信息：{Path.GetDirectoryName(_directory_name)}\");\n",
    "\n",
    "// 获取文件名\n",
    "string _file_name = Path.GetFileName(_path);\n",
    "Console.WriteLine($\"文件名信息：{_file_name}\");\n",
    "string _file_without_extension_name = Path.GetFileNameWithoutExtension(_path);\n",
    "Console.WriteLine($\"文件名信息：{_file_without_extension_name}\");\n",
    "string _extension_name = Path.GetExtension(_path);\n",
    "Console.WriteLine($\"扩展名信息：{_extension_name}\");\n",
    "\n",
    "// 从相对路径获取绝对路径\n",
    "// 这里就是获取当前路径，然后使用Combine进行组合，就算文件不存在，也可以获得绝对路径\n",
    "string _full_path = Path.GetFullPath(\"./1.txt\");\n",
    "Console.WriteLine($\"绝对路径：{_full_path}\");\n",
    "\n",
    "// 创建临时文件/临时路径\n",
    "string _temp_path = Path.GetTempFileName();\n",
    "Console.WriteLine($\"临时路径：{_temp_path}\");\n",
    "string _temp_file = Path.GetRandomFileName();\n",
    "Console.WriteLine($\"临时文件：{_temp_file}\");\n",
    "string _temp_directory = Path.GetTempPath();\n",
    "Console.WriteLine($\"临时文件夹：{_temp_directory}\");\n",
    "\n",
    "// 检查路径是否包含扩展名\n",
    "bool _has_extension1 = Path.HasExtension(_path);\n",
    "bool _has_extension2 = Path.HasExtension(_directory_name);\n",
    "bool _has_extension3 = Path.HasExtension(_file_name);\n",
    "Console.WriteLine($\"HasExtension1：{_has_extension1}\");\n",
    "Console.WriteLine($\"HasExtension2：{_has_extension2}\");\n",
    "Console.WriteLine($\"HasExtension3：{_has_extension3}\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 一些特殊的文件夹路径获取（`Environment.SpecialFolder`）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "C:\\Users\\zeros\\AppData\\Local"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData).Display();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 文件夹 `Directory`\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "165\n",
      "198\n",
      "363\n",
      "c:\\Users\\zeros\\Documents\\【项目文件夹】\\【常用】草稿文档\\CSharp\\02_CSharp基础\n",
      "c:\\Users\\zeros\\Documents\\【项目文件夹】\\【常用】草稿文档\\CSharp\n"
     ]
    }
   ],
   "source": [
    "// 创建指定的目录，如果文件夹已存在，则会抛出异常\n",
    "string _directory = Path.Combine(Path.GetTempPath(), \"Directory\", \"Test\");\n",
    "DirectoryInfo _directory_info = Directory.CreateDirectory(_directory);\n",
    "Console.WriteLine($\"{Path.Exists(_directory)}\");\n",
    "\n",
    "// 删除指定目录\n",
    "Directory.Delete(_directory);\n",
    "Console.WriteLine($\"{Path.Exists(_directory)}\");\n",
    "\n",
    "// 获取当前目录下的文件/文件夹\n",
    "string[] _directory_array = Directory.GetDirectories($\"{Path.GetTempPath()}\");\n",
    "Console.WriteLine($\"{_directory_array.Length}\");\n",
    "string[] _file_array = Directory.GetFiles($\"{Path.GetTempPath()}\");\n",
    "Console.WriteLine($\"{_file_array.Length}\");\n",
    "string[] _entries_array = Directory.GetFileSystemEntries($\"{Path.GetTempPath()}\");\n",
    "Console.WriteLine($\"{_entries_array.Length}\");\n",
    "\n",
    "// 获取当前目录\n",
    "string _current_directory = Directory.GetCurrentDirectory();\n",
    "Console.WriteLine($\"{_current_directory}\");\n",
    "// 获取父目录\n",
    "DirectoryInfo _parent_directory_info = Directory.GetParent(_current_directory);\n",
    "Console.WriteLine($\"{_parent_directory_info.FullName}\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 文件 `File`\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Users\\zeros\\AppData\\Local\\Temp\\Directory\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "// 创建文件，\n",
    "string _file_path = Path.Combine(Path.GetTempPath(), \"Directory\", \"1.txt\");\n",
    "// 确保父目录存在\n",
    "string _directory_path = Path.GetDirectoryName(_file_path);\n",
    "_directory_path.Dump();\n",
    "Directory.CreateDirectory(_directory_path);\n",
    "// 此处并不会创建父目录的文件夹，如果父目录不存在，则 FileCreate 会抛出异常\n",
    "FileStream _file_stream = File.Create(_file_path);\n",
    "_file_stream.Close(); // Create 后会打开文件流，文件会被占用，因此要关闭\n",
    "Console.WriteLine($\"{File.Exists(_file_name)}\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bbb\r\n"
     ]
    }
   ],
   "source": [
    "string _message = \"aaabbb\";\n",
    "_message.Replace(\"a\", null).Dump();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temp\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "string _path = Path.Combine(Path.GetTempPath(), \"Directory\", \"Text\", \"Temp\");\n",
    "Directory.CreateDirectory(_path);\n",
    "Directory.CreateDirectory(_path);\n",
    "Directory.CreateDirectory(_path);\n",
    "\n",
    "string _file_path = Path.Combine(_path, \"1.txt\");\n",
    "File.Create(_file_path).Close();\n",
    "File.Create(_file_path).Close();\n",
    "File.Create(_file_path).Close();\n",
    "\n",
    "Path.GetFileName(_path).Dump();\n",
    "\n",
    "File.Exists(_file_path).Dump();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [],
   "source": [
    "File.AppendAllLines(_file_path, [\"Hello World\", \"Hello World\"]);\n",
    "File.AppendAllText(_file_path, \"Hello World\");\n",
    "File.AppendAllText(_file_path, \"Hello World\");\n",
    "File.AppendAllText(_file_path, \"Hello World\");\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\r\n"
     ]
    }
   ],
   "source": [
    "Path.GetFileNameWithoutExtension(\"1\").Dump()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100\r\n"
     ]
    }
   ],
   "source": [
    "\n",
    "byte[] @bytes = Encoding.Default.GetBytes(\"Hello World\");\n",
    "@bytes.Dump();\n",
    "File.WriteAllBytes(_file_path, @bytes);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100\r\n"
     ]
    }
   ],
   "source": [
    "File.ReadAllBytes(_file_path).Dump();"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "name": "polyglot-notebook"
  },
  "polyglot_notebook": {
   "kernelInfo": {
    "defaultKernelName": "csharp",
    "items": [
     {
      "aliases": [],
      "languageName": "csharp",
      "name": "csharp"
     }
    ]
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
